import {
  BASE_URL
} from '../../apis/requests.js';
// pages/userAdmin/userAdmin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picker: ["全部", "按用户名"], //与index联系
    index: 0,
    users: [],
    searchName: ""
  },

  PickerChange(e) {
    this.setData({
      index: parseInt(e.detail.value)
    })
  },

  inputChange(e) {
    this.setData({
      searchName: e.detail.value
    })
  },

  search() {
    if (this.data.index === 0) {
      const values = {
        "token": wx.getStorageSync('token')
      }
      wx.request({
        url: BASE_URL + "/admin/find_all_users",
        data: values,
        header: {
          'content-type': 'application/json'
        },
        method: 'POST',
        dataType: 'json',
        success: (result) => {
          console.log(result)
          if (result.data.code === "200") {
            this.setData({
              users: result.data.data
            })

          }
        },
        fail: () => {},
        complete: () => {}
      })

    } else if (this.data.index === 1) {

      const values = {
        "token": wx.getStorageSync('token'),
        "id": this.data.searchName
      }
      wx.request({
        url: BASE_URL + "/admin/find_user_by_id",
        data: values,
        header: {
          'content-type': 'application/json'
        },
        method: 'POST',
        dataType: 'json',
        success: (result) => {
          console.log(result)
          if (result.data.code === "200") {
            this.setData({
              users: result.data.data
            })

          }
        },
        fail: () => {},
        complete: () => {}
      })
    }



  },

  bindChangeState(e) {
    const that = this

    wx.showModal({
      title: "提示",
      content: "确定修改吗？",
      success(res) {
        if (res.confirm) {
          //console.log(e.currentTarget.dataset.index)
          const values = {
            "token": wx.getStorageSync('token'),
            "id": that.data.users[e.currentTarget.dataset.index].id,
            "state": that.data.users[e.currentTarget.dataset.index].state ? 0 : 1
          }
          wx.request({
            url: BASE_URL + "/admin/update_user_auth",
            data: values,
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            dataType: 'json',
            success: (result) => {
              console.log(result)
              if (result.data.code === 200) {
                wx.showModal({
                  title: "提示",
                  content: "操作成功!",
                  showCancel: false,
                  success(res) {}
                })
              } else {
                wx.showModal({
                  title: "提示",
                  content: result.data.msg,
                  showCancel: false,
                  success(res) {}
                })
              }

            },
            fail: () => {},
            complete: () => {}
          })
        }
      }
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})